package co.yixiang.modules.tools.rest;

import co.yixiang.api.ApiResult;
import co.yixiang.vod.config.VodProperties;
import co.yixiang.vod.service.VodTemplate;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.DescriptionResolver;

/**
 * 阿里云视频点播管理控制器
 * <p>
 * 该控制器提供阿里云视频点播服务的相关接口，包括：
 * 1. 获取视频播放凭证
 * 2. 获取视频上传地址和凭证
 * 3. 刷新视频上传凭证
 * </p>
 */
@Api(tags = "阿里云视频点播管理")
@RestController
@RequestMapping("/api/aliVod")
@Slf4j
public class AliVodController {

    /**
     * 阿里云视频点播配置属性
     */
    private final VodProperties vodProperties;

    /**
     * 阿里云视频点播服务模板
     */
    private final VodTemplate vodTemplate;

    /**
     * Swagger描述解析器
     */
    private final DescriptionResolver descriptionResolver;

    /**
     * 构造函数
     *
     * @param vodProperties 阿里云视频点播配置属性
     * @param vodTemplate 阿里云视频点播服务模板
     * @param descriptionResolver Swagger描述解析器
     */
    public AliVodController(VodProperties vodProperties, VodTemplate vodTemplate, DescriptionResolver descriptionResolver) {
        this.vodProperties = vodProperties;
        this.vodTemplate = vodTemplate;
        this.descriptionResolver = descriptionResolver;
    }

    /**
     * 获取阿里云视频播放凭证
     * <p>
     * 通过视频ID获取播放凭证，用于视频播放鉴权
     * </p>
     *
     * @param videoId 视频ID
     * @return 包含播放凭证信息的API响应
     */
    @ApiOperation("获取阿里云视频播放凭证")
    @GetMapping("/getPlayInfo/{videoId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name ="videoId", value = "视频ID", paramType = "path", required = true, dataType = "String")
    })
    public ApiResult<Object> getPlayInfo(@PathVariable(value = "videoId") String videoId) {
        return ApiResult.ok(vodTemplate.getPlayInfo(videoId));
    }

    /**
     * 获取视频上传地址和凭证
     * <p>
     * 用于前端JavaScript SDK上传视频时获取必要的上传凭证和地址信息
     * </p>
     *
     * @param title 视频标题
     * @param filename 视频文件名
     * @param description 视频描述
     * @return 包含上传地址和凭证的API响应
     */
    @GetMapping("/token")
    @ApiOperation(value = "获取视频上传地址和凭证",notes = "管理后台调用阿里VOD JavaScript sdk 获取视频上传地址和凭证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "文件标题 例：this a sample", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "filename", value = "文件名 例：filename.mp4", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "description", value = "视频描述", dataType = "String", paramType = "query", required = true)
    })
    public ApiResult<CreateUploadVideoResponse> getAliyunVodUploadToken (@RequestParam(value = "title") String title,
                                                                         @RequestParam(value = "filename") String filename,
                                                                         @RequestParam(value = "description") String description) {

        CreateUploadVideoResponse response = vodTemplate.createUploadVideo(title, filename, description);

//        log.info("VideoId = " + response.getVideoId() + "\n");
//        log.info("UploadAddress = " + response.getUploadAddress() + "\n");
//        log.info("UploadAuth = " + response.getUploadAuth() + "\n");
//        log.info("RequestId = " + response.getRequestId() + "\n");

        return ApiResult.ok(response);
    }

    /**
     * 刷新视频上传凭证
     * <p>
     * 当上传凭证超时后，可以通过该接口重新获取上传凭证
     * </p>
     *
     * @param videoId 视频ID
     * @return 包含刷新后的上传凭证的API响应
     */
    @GetMapping("/token/{videoId}/refresh")
    @ApiOperation(value = "刷新视频上传凭证",notes = "管理后台调用阿里VOD JavaScript sdk 刷新视频上传地址和凭证")
    @ApiImplicitParams({
            @ApiImplicitParam(name ="videoId", value = "视频ID", paramType = "path", required = true, dataType = "String")
    })
    public ApiResult<RefreshUploadVideoResponse> refreshAliyunVodUploadToken (@PathVariable(value = "videoId") String videoId) {

        RefreshUploadVideoResponse response = vodTemplate.refreshUploadVideo(videoId);

//        log.info("VideoId = " + response.getVideoId() + "\n");
//        log.info("UploadAddress = " + response.getUploadAddress() + "\n");
//        log.info("UploadAuth = " + response.getUploadAuth() + "\n");

        return ApiResult.ok(response);
    }

}
